-
Notifications
You must be signed in to change notification settings - Fork 490
/
validation-aggregate.component.ts
225 lines (193 loc) · 6.85 KB
/
validation-aggregate.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* @license
* Copyright 2018 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, HostListener, Input} from "@angular/core";
import {
CombinedAllNodeVisitor,
Node,
TraverserDirection,
ValidationProblem,
ValidationProblemSeverity,
VisitorUtil
} from "apicurio-data-models";
import {ProblemsService} from "../../_services/problems.service";
import {DocumentService} from "../../_services/document.service";
import {AbstractBaseComponent} from "./base-component";
import {SelectionService} from "../../_services/selection.service";
import {ArrayUtils} from "apicurio-ts-core";
import {KeypressUtils} from "../../_util/keypress.util";
@Component({
moduleId: module.id,
selector: "validation-aggregate",
templateUrl: "validation-aggregate.component.html",
styleUrls: [ "validation-aggregate.component.css" ],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ValidationAggregateComponent extends AbstractBaseComponent {
protected _models: Node[];
@Input()
set models(models: Node[]) {
if (!ArrayUtils.equals(models, this._models)) {
this._problems = undefined;
this._models = models;
}
}
get models(): Node[] {
return this._models;
}
@Input() properties: string[];
@Input() codes: string[];
@Input() shallow: boolean;
@Input() debug: boolean;
private _open: boolean = false;
public left: string;
public top: string;
private _problems: ValidationProblem[] = undefined;
constructor(private changeDetectorRef: ChangeDetectorRef, private documentService: DocumentService,
private problemsService: ProblemsService, private selectionService: SelectionService) {
super(changeDetectorRef, documentService, selectionService);
}
protected onDocumentChange(): void {
this.log("Invalidating cache.");
this._problems = undefined;
}
@HostListener("document:click", ["$event"])
public onDocumentClick(event: MouseEvent): void {
if (this._open) {
this.close();
event.preventDefault();
event.stopPropagation();
}
}
@HostListener("document:scroll", ["$event"])
public onDocumentScroll(event: MouseEvent): void {
this.onDocumentClick(event);
}
@HostListener("window:resize", ["$event"])
public onWindowResize(event: MouseEvent): void {
this.onDocumentClick(event);
}
public open(event: MouseEvent): void {
this.left = event.clientX + "px";
this.top = event.clientY + "px";
event.preventDefault();
event.stopPropagation();
this._open = true;
}
public close(): void {
this._open = false;
}
public isOpen(): boolean {
return this._open;
}
/**
* Called whenever the user presses a key.
* @param event
*/
public onGlobalKeyDown(event: KeyboardEvent): void {
if (KeypressUtils.isEscapeKey(event)) {
this.close();
}
}
public hasProblems(): boolean {
if (!this.models) {
return false;
}
return this.matchingProblems().length > 0;
}
public matchingProblems(): ValidationProblem[] {
if (!this.models) {
return [];
}
// Find the relevant problems if not cached.
if (this._problems === undefined) {
this.log("Cache empty, finding matching problems.");
let finder: ProblemFinder = new ProblemFinder(this.properties, this.codes);
for (let model of this.models) {
if (model !== null && model !== undefined) {
if (this.shallow) {
VisitorUtil.visitNode(model, finder);
} else {
VisitorUtil.visitTree(model, finder, TraverserDirection.down);
}
}
}
this._problems = finder.getProblems();
}
return this._problems;
}
public iconFor(problem: ValidationProblem): string {
switch (problem.severity) {
case ValidationProblemSeverity.low:
return "pficon-info";
case ValidationProblemSeverity.medium:
return "pficon-warning-triangle-o";
case ValidationProblemSeverity.high:
return "pficon-error-circle-o";
default:
return "";
}
}
public summaryFor(problem: ValidationProblem): string {
return this.problemsService.summary(problem);
}
public goTo(problem: ValidationProblem, event: MouseEvent): void {
this.close();
let goToPath: string = problem.nodePath.toString();
if (problem.property) {
goToPath += "/" + problem.property;
}
this.__selectionService.select(goToPath);
// Delay the highlighting of the path so that the UI has a chance to display
// the correct components for the new selection (see above).
setTimeout( () => {
this.__selectionService.highlightPath(goToPath);
}, 50);
event.preventDefault();
event.stopPropagation();
}
protected log(message: string): void {
if (this.debug) {
console.info("[ValidationAggregateComponent] " + message);
}
}
}
export class ProblemFinder extends CombinedAllNodeVisitor {
private problems: ValidationProblem[] = [];
constructor(private properties: string[], private codes: string[]) {
super();
}
public getProblems(): ValidationProblem[] {
return this.problems;
}
visitNode(node: Node): void {
node.getValidationProblems().forEach( problem => {
if (this.accepts(problem)) {
this.problems.push(problem);
}
});
}
private accepts(problem: ValidationProblem): boolean {
let accept: boolean = true;
if (this.properties !== null && this.properties !== undefined && this.properties.length > 0) {
accept = accept && this.properties.indexOf(problem.property) != -1;
}
if (this.codes !== null && this.codes !== undefined && this.codes.length > 0) {
accept = accept && this.codes.indexOf(problem.errorCode) != -1;
}
return accept;
}
}